<?php

namespace Api\Controller;
use Api\Common\BaseController;
use Common\helpers\Helper;

class QuestionController extends BaseController
{
    
    /* 
     * 问题列表
     *  */
    public function questionList(){
        $where = "`id` > '0'";
        //是否只查询个人问题
        if($_POST['is_personal'] == 'y'){
            $where .= " and `user_id`='".$this->user_id."'";
        }
        
        $data = M('questionList')->where($where)->field('id,title,type,create_time')->order('id desc')->select();
        echo json_encode($this->responseSuccess($data));
    }
    
    /* 
     * 添加问题
     *  */
    public function quesstionPost(){
        
        //验证
        if(!(preg_match(Helper::checkMobile(),$_POST['mobile']))){
            echo json_encode($this->responseError('手机号错误'));die;
        }
        
        //添加
        $data = $_POST;
        $data['user_id'] = $this->user_id;
        $data['create_time'] = date("Y-m-d H:i:s");
        $sta = M('questionList')->add($data);
        
        //返回
        $response = $sta ? $this->responseSuccess(null) : $this->responseError('添加失败');
        echo json_encode($response);
        
    }
    
    /*
     * 问题详情
     *  */
    public function quesstionInfo(){
        $ques_id = $_POST['question_id'];
        if(!$ques_id){
            echo json_encode($this->responseError('参数错误'));
        }else{
            $data = M('questionList')->where(array('id'=>$ques_id))->find();
            $data['userInfo'] = M('user')->where(array('id'=>$data['user_id']))->field('id,username,headpic')->find();
            $data['answerInfo'] = M('user')->where(array('id'=>$data['answer_id']))->field('id,username,headpic')->find();
            echo json_encode($this->responseSuccess($data));
        }
        
    }
    
    /* 
     * 回答者选中回答的问题
     *  */
    public function questionAnswer(){
        
        //修改
        $ques_id = $_POST['question_id'];
        $save = array(
            'answer_id'=>$this->user_id,
            'answer_time'=>date("Y-m-d H:i:s"),
        );
        $sta = M('questionList')->where(array('id'=>$ques_id))->save($save);
        
        //返回
        $response = $sta ? $this->responseSuccess(null) : $this->responseError('添加失败');
        echo json_encode($response);
    }
    
}

?>